package game;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;


public class Renderer2D extends JFrame implements Renderer, MouseListener{
	Checkers checkers;
	BufferedImage bi = new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB);
	Graphics2D big;
	int clickedX;
	int clickedY;
	public Renderer2D(Checkers checkers){
		super();
		setTitle("Checkers");
		setBackground(Color.white);
		setPreferredSize(new Dimension(650, 675));
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
		pack();
		this.checkers = checkers;
	}
	public void init(){
		bi = (BufferedImage) createImage(600, 600);
	    big = bi.createGraphics();
	    clickedX = -1;
	    clickedY = -1;
	    addMouseListener(this);
	}
	@Override
	public void draw() {
		for (int i = 0; i < Checkers.WIDTH; i++) {
			for (int j = 0; j < Checkers.HEIGHT; j++) {
				if(i % 2 == 0){
					if(j % 2 == 0)big.setColor(new Color(255, 206, 158)); else big.setColor(new Color(209, 139, 71));					
				}
				if(i % 2 == 1){
					if(j % 2 == 1)big.setColor(new Color(255, 206, 158)); else big.setColor(new Color(209, 139, 71));					
				}
				big.fillRect(j*75, i*75, 75, 75);
			}
		}
		for (Piece piece : checkers.getPieces()) {
			if(checkers.getSelectedPiece()!=null)
			if(piece.getX() == checkers.getSelectedPiece().getX() && piece.getY() == checkers.getSelectedPiece().getY()) {
				big.setColor(new Color(0, 0, 0));
				big.fillOval(piece.getX()*75+5, piece.getY()*75+5, 65, 65);
			}
			if(piece.isWhite())big.setColor(new Color(255, 249, 244)); else big.setColor(new Color(106, 0, 3));
			big.fillOval(piece.getX()*75+10, piece.getY()*75+10, 55, 55);
			if(piece.isKing()){
			if(piece.isWhite())big.setColor(new Color(100, 100, 100)); else big.setColor(new Color(255, 0, 0));						
				big.fillOval(piece.getX()*75+20, piece.getY()*75+20, 35, 35);
			}
		}		
		if(checkers.getSelectedX()!=-1){
			if(checkers.getSelectedY() % 2 == 0){
				if(checkers.getSelectedX() % 2 == 0)big.setColor(new Color(205, 206, 158)); else big.setColor(new Color(249, 139, 71));					
			}
			if(checkers.getSelectedY() % 2 == 1){
				if(checkers.getSelectedX() % 2 == 1)big.setColor(new Color(205, 206, 158)); else big.setColor(new Color(249, 139, 71));					
			}
			big.fillRect(checkers.getSelectedX()*75, checkers.getSelectedY()*75, 75, 75);
		}
			
		getGraphics().drawImage(bi, 25, 50, this);
	}
	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mousePressed(MouseEvent arg0) {
		clickedX = (arg0.getX()-25) / 75;
		clickedY = (arg0.getY()-50) / 75;		
	}
	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public Piece clickedPiece() {
		for (Piece piece: checkers.getPieces()) {
			if(piece.getX() == clickedX && piece.getY() == clickedY) return piece;
		}
		return null;
	}
	public int getClickedX() {
		return clickedX;
	}
	public int getClickedY() {
		return clickedY;
	}
}
